package cn.otrue.patienthealthmanager.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.GrenxinxiAdpater;
import cn.otrue.patienthealthmanager.bean.ThreadModel;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.view.XListView;
import cn.otrue.patienthealthmanager.view.XListView.IXListViewListener;

import com.google.gson.reflect.TypeToken;

/**
 * 健康社区
 * 
 * @author G
 * 
 */
public class WodeActivity extends BaseActivity implements IXListViewListener {
	private int currentPage = 1;
	private List<ThreadModel> goods;
	private GrenxinxiAdpater postAdapter;
	private XListView id_wode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wode_tiezi);

		initView();
		initTitleBar();
	}

	public void initTitleBar() {
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(R.string.my_post);

		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	private void initView() {
		goods = new ArrayList<ThreadModel>();
		id_wode = (XListView) findViewById(R.id.id_wode);
		id_wode.setPullLoadEnable(false); // 是否开启上拉加载
		id_wode.setXListViewListener(this);// 上啦加载和下拉刷新的监听
		getfatie(currentPage);
		id_wode.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				int threadId = goods.get(position - 1).getThreadId();

				Intent intent = new Intent();
				intent.putExtra("threadId", threadId);
				intent.setClass(WodeActivity.this,
						CommunityPostInfoActivity.class);
				startActivity(intent);

			}
		});

	}

	private Handler fatieHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			onLoad();
			if (state.equals("001")) {
				String tuu = data.getString(HttpCons.DATA);
				Log.e("liyu", tuu);
				List<ThreadModel> iiii = HttpServer.gson.fromJson(tuu,
						new TypeToken<List<ThreadModel>>() {
						}.getType());
				goods.addAll(iiii);
				if (goods.size() > 5) {
					id_wode.setPullLoadEnable(true);
				}
				if (goods.size() != 0) {

					postAdapter = new GrenxinxiAdpater(goods, WodeActivity.this);
					id_wode.setAdapter(postAdapter);
				}

				Toast.makeText(WodeActivity.this, "操作成功", 0).show();
			} else {
				onLoad();
				Toast.makeText(WodeActivity.this, "msg", 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			onLoad();
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(WodeActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private void getfatie(int i) {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("aliasAccount", MyConst.getACCOUNT());
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername(MyConst.getACCOUNT());
		parameter.setPassword(" ");

		parameter.setCurrentPage(i + "");
		parameter.setPagesize("20");
		parameter.setData(map);
		new HttpServer().post(HttpCons.query_Thread_List, parameter,
				fatieHandler);

	}

	private void onLoad() {
		id_wode.stopRefresh();
		id_wode.stopLoadMore();
		id_wode.setRefreshTime("刚刚");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		goods.clear();
		currentPage = 1;

		getfatie(currentPage);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub

		currentPage++;

		getfatie(currentPage);
	}

}
